/*
 * WantBuyModel.java
 * business
 *
 * Created by ChenTao on 2017/3/2.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.data.model;

import android.graphics.Bitmap;

import com.yidiandao.business.constant.Common;
import com.yidiandao.business.constant.Urls;
import com.yidiandao.business.data.bean.Goods;
import com.yidiandao.business.data.event.RefreshChooseGoodsEvent;
import com.yidiandao.business.data.event.RefreshWantBuyDetailEvent;
import com.yidiandao.business.data.net.BaseResponse;
import com.yidiandao.business.data.net.NetBiz;
import com.yidiandao.business.data.net.NetListener;
import com.yidiandao.business.data.net.exception.BizErrorInfo;
import com.yidiandao.business.data.net.exception.NetErrorInfo;
import com.yidiandao.business.data.request.PagerRequest;
import com.yidiandao.business.data.request.UploadLocalOfferImageRequest;
import com.yidiandao.business.data.request.WantOfferRequest;
import com.yidiandao.business.data.response.AgentResponse;
import com.yidiandao.business.manager.ChooseGoodsManager;
import com.yidiandao.business.utils.FileUtils;
import com.yidiandao.business.utils.PictureUtils;
import com.yidiandao.business.utils.PreferenceUtils;
import com.yidiandao.business.utils.UIUtils;

import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/** 求购Model */
public class WantBuyModel implements IWantBuyModel {

    private NetBiz mNetBiz = new NetBiz();

    private List<Goods> mAgentGoodsList = new ArrayList<>();

    private int agentGoodsListPager;

    @Override
    public void loadAgentGoodsList(
            final boolean isRefreshing,
            String tag,
            final OnAccessNetListener<List<Goods>> listener) {

        if (isRefreshing) {
            agentGoodsListPager = 1;
        }

        PagerRequest request = new PagerRequest();
        request.setUrl(Urls.PROXY_GOODS);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setPage(agentGoodsListPager);
        request.setPageSize(Common.PAGER_SIZE);

        mNetBiz.sendGetRequest(request, AgentResponse.class, new NetListener<AgentResponse>() {
            @Override
            public void onSuccess(AgentResponse response) {

                agentGoodsListPager++;

                if (isRefreshing) {
                    mAgentGoodsList.clear();
                }

                // 代理商数据：包含 --> 店铺 、 店铺商品列表
                List<AgentResponse.Data.Agent> agents = response.getData().getAgent();

                for (int i = 0; i < agents.size(); i++) {
                    AgentResponse.Data.Agent agent = agents.get(i);
                    List<Goods> goodsList = agent.getGoodsList();
                    mAgentGoodsList.addAll(goodsList);
                }

                if (listener != null) {
                    listener.onSuccess(
                            mAgentGoodsList, agents.size() == 0, response.hasMoreData());
                }

                // 将代理的商品设置到选择的商品列表
                ChooseGoodsManager.getInstance().initGoodsList(mAgentGoodsList);
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void postRefreshChooseGoodsEvent() {
        EventBus.getDefault().post(new RefreshChooseGoodsEvent());
    }

    @Override
    public void postRefreshWantBuyDetailEvent() {
        EventBus.getDefault().post(new RefreshWantBuyDetailEvent());
    }

    @Override
    public void wantOfferFromAgent(
            String wid,
            String goodsId,
            String price,
            String phoneNumber,
            String tag,
            final OnAccessNetListener listener) {

        WantOfferRequest request = new WantOfferRequest();
        request.setUrl(Urls.WANT_OFFER);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setWid(wid);
        request.setGoodsId(goodsId);
        request.setPrice(price);
        request.setContactNumber(phoneNumber);

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, true);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void wantOfferFromLocal(
            String wid,
            long timestamp,
            String price,
            String phoneNumber,
            String tag,
            final OnAccessNetListener listener) {

        WantOfferRequest request = new WantOfferRequest();
        request.setUrl(Urls.WANT_OFFER);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setTag(tag);
        request.setWid(wid);
        request.setPrice(price);
        request.setContactNumber(phoneNumber);
        request.setAlbumId(timestamp + "");

        mNetBiz.sendPostRequest(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, true);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public void uploadLoadImages(
            String wid,
            long timestamp,
            final File file,
            String tag,
            final OnAccessNetListener listener) {

        UploadLocalOfferImageRequest request = new UploadLocalOfferImageRequest();
        request.setUrl(Urls.WANT_OFFER_UPLOAD);
        request.setWid(wid);
        request.setTag(tag);
        request.setToken(PreferenceUtils.getToken(UIUtils.getContext()));
        request.setAlbumId(timestamp + "");
        // 添加需要上传的图片
        request.addUploadFile("picture", file);

        mNetBiz.uploadFiles(request, BaseResponse.class, new NetListener<BaseResponse>() {
            @Override
            public void onSuccess(BaseResponse response) {
                if (listener != null) {
                    listener.onSuccess(null, false, true);
                }
            }

            @Override
            public boolean onError(NetErrorInfo netErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return false;
            }

            @Override
            public boolean onFailed(BizErrorInfo bizErrorInfo) {
                if (listener != null) {
                    listener.onFailed();
                }
                return true;
            }

            @Override
            public void onFinal() {
                if (listener != null) {
                    listener.onFinal();
                }
            }
        });
    }

    @Override
    public File pressImage(String path) {
        //压缩图片
        Bitmap bm = PictureUtils.getSmallBitmap(path, 480, 800);
        //保存图片
        return FileUtils.saveBitmapFile(bm, "/upload_" + System.currentTimeMillis() + ".jpg");
    }
}
